package com.fc.controller;


import com.fc.anno.Log;
import com.fc.context.BaseContext;
import com.fc.dto.AssessmentDTO;
import com.fc.dto.pageQuery.AssessmentPageQueryDTO;
import com.fc.entity.Assessment;
import com.fc.result.PageResult;
import com.fc.result.Result;
import com.fc.service.IAssessmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "考核")
@RestController
@RequestMapping("/assessment")
public class AssessmentController {

    @Autowired
    private IAssessmentService assessmentService;

    /**
     * 添加考核记录
     *
     * @param assessmentDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("新增考核记录")
    @PostMapping
    public Result save(@RequestBody AssessmentDTO assessmentDTO) {
        assessmentService.savaAssessment(assessmentDTO);
        return Result.success();
    }

    /**
     * 修改考核记录
     *
     * @param assessmentDTO
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("修改考核记录")
    @PutMapping
    public Result update(@RequestBody AssessmentDTO assessmentDTO) {
        assessmentService.updateAssessment(assessmentDTO);
        return Result.success();
    }

    /**
     * 删除考核记录
     *
     * @param id
     * @return
     */
    @Log
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("删除考核记录")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        assessmentService.removeById(id);
        return Result.success();
    }

    /**
     * 管理员查看考核记录列表
     *
     * @param assessmentPageQueryDTO
     * @return
     */
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("查看考核记录列表")
    @PostMapping("/list")
    public Result<PageResult> list(@RequestBody AssessmentPageQueryDTO assessmentPageQueryDTO) {
        PageResult pageResult = assessmentService.listAssessment(assessmentPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 司机查看自己的考核记录
     *
     * @return
     */
    @ApiOperation("司机查看自己的考核记录")
    @GetMapping
    public Result<List<Assessment>> detail() {
        List<Assessment> assessmentList = assessmentService.detail(BaseContext.getCurrentId());
        return Result.success(assessmentList);
    }

}
